// 特效管理器
import * as PIXI from 'pixi.js'

export default class EffectManager {
  constructor(scene) {
    this.scene = scene
    this.particleContainer = new PIXI.ParticleContainer()
    this.scene.addChild(this.particleContainer)
    this.effects = []
    this.sounds = {}
    
    // 加载默认音效
    this.loadSound('collision', './static/sounds/collision.wav')
    this.loadSound('explosion', './static/sounds/explosion.wav')
    this.loadSound('gravity', './static/sounds/gravity.wav')
    this.loadSound('terrain', './static/sounds/terrain.wav')
  }

  // 加载音效
  async loadSound(name, url) {
    const response = await fetch(url)
    const arrayBuffer = await response.arrayBuffer()
    const audioBuffer = await new AudioContext().decodeAudioData(arrayBuffer)
    this.sounds[name] = audioBuffer
  }

  // 播放音效
  playSound(name, volume = 1.0) {
    if (!this.sounds[name]) return
    
    const source = new AudioContext().createBufferSource()
    source.buffer = this.sounds[name]
    
    const gainNode = new AudioContext().createGain()
    gainNode.gain.value = volume
    
    source.connect(gainNode)
    gainNode.connect(new AudioContext().destination)
    source.start(0)
  }

  // 创建引力场效果
  createGravityField(position, radius) {
    const field = new PIXI.Graphics()
    field.beginFill(0x00FFFF, 0.3)
    field.drawCircle(0, 0, radius)
    field.endFill()
    field.position.set(position.x, position.y)
    
    // 添加波动动画
    field.alpha = 0.5
    field.scale.set(1)
    field.animate = (delta) => {
      field.alpha = 0.3 + Math.sin(Date.now() * 0.002) * 0.2
      field.scale.x = 1 + Math.sin(Date.now() * 0.001) * 0.1
      field.scale.y = 1 + Math.cos(Date.now() * 0.001) * 0.1
    }
    
    this.scene.addChild(field)
    this.effects.push(field)
    this.playSound('gravity', 0.5)
    return field
  }

  // 创建拖尾效果
  createTrailEffect(target) {
    const trail = new PIXI.ParticleContainer(100, {
      scale: true,
      position: true,
      rotation: true,
      uvs: true,
      alpha: true
    })
    
    // 添加粒子发射器
    const emitter = new PIXI.particles.Emitter(
      trail,
      [PIXI.Texture.WHITE],
      {
        alpha: {
          start: 0.8,
          end: 0
        },
        scale: {
          start: 0.5,
          end: 0.2,
          minimumScaleMultiplier: 1
        },
        color: {
          start: target.color,
          end: 'ffffff'
        },
        speed: {
          start: 50,
          end: 0,
          minimumSpeedMultiplier: 1
        },
        acceleration: {
          x: 0,
          y: 0
        },
        maxSpeed: 0,
        startRotation: {
          min: 0,
          max: 360
        },
        noRotation: false,
        rotationSpeed: {
          min: 0,
          max: 0
        },
        lifetime: {
          min: 0.5,
          max: 1
        },
        blendMode: 'normal',
        frequency: 0.01,
        emitterLifetime: -1,
        maxParticles: 100,
        pos: {
          x: 0,
          y: 0
        },
        addAtBack: false,
        spawnType: 'circle',
        spawnCircle: {
          x: 0,
          y: 0,
          r: 10
        }
      }
    )
    
    emitter.emit = true
    this.scene.addChild(trail)
    this.effects.push({
      container: trail,
      emitter,
      update: (delta) => {
        emitter.update(delta * 0.01)
        trail.position.set(target.position.x, target.position.y)
      }
    })
    this.playSound('terrain', 0.3)
    return trail
  }

  // 创建能量传递效果
  createEnergyTransferEffect(from, to) {
    const line = new PIXI.Graphics()
    line.lineStyle(2, 0x00FF00, 0.8)
    
    // 添加动画
    line.animate = (delta) => {
      line.clear()
      line.lineStyle(2, 0x00FF00, 0.8)
      line.moveTo(from.position.x, from.position.y)
      line.lineTo(to.position.x, to.position.y)
      
      // 添加流动效果
      const offset = Date.now() % 1000 / 1000
      line.lineTextureStyle({
        width: 2,
        texture: PIXI.Texture.from('energyLine'),
        color: 0x00FF00,
        alpha: 0.8,
        offset: offset
      })
    }
    
    this.scene.addChild(line)
    this.effects.push(line)
    return line
  }

  // 创建碰撞火花效果
  createCollisionSpark(position) {
    const spark = new PIXI.ParticleContainer(50, {
      scale: true,
      position: true,
      rotation: true,
      uvs: true,
      alpha: true
    })
    
    const emitter = new PIXI.particles.Emitter(
      spark,
      [PIXI.Texture.WHITE],
      {
        alpha: {
          start: 1,
          end: 0
        },
        scale: {
          start: 0.5,
          end: 0.1,
          minimumScaleMultiplier: 1
        },
        color: {
          start: 'ffffff',
          end: 'ff6600'
        },
        speed: {
          start: 200,
          end: 50,
          minimumSpeedMultiplier: 1
        },
        acceleration: {
          x: 0,
          y: 0
        },
        maxSpeed: 0,
        startRotation: {
          min: 0,
          max: 360
        },
        noRotation: false,
        rotationSpeed: {
          min: 0,
          max: 0
        },
        lifetime: {
          min: 0.2,
          max: 0.5
        },
        blendMode: 'add',
        frequency: 0.001,
        emitterLifetime: 0.2,
        maxParticles: 50,
        pos: {
          x: position.x,
          y: position.y
        },
        addAtBack: false,
        spawnType: 'circle',
        spawnCircle: {
          x: 0,
          y: 0,
          r: 10
        }
      }
    )
    
    emitter.emit = true
    this.scene.addChild(spark)
    this.effects.push({
      container: spark,
      emitter,
      update: (delta) => {
        emitter.update(delta * 0.01)
        if (emitter.emitterLifetime > 0 && emitter.emitterLifetime <= delta * 0.01) {
          this.scene.removeChild(spark)
        }
      }
    })
    this.playSound('collision', 0.8)
    return spark
  }

  // 创建终点爆炸效果
  createGoalExplosion(position) {
    const explosion = new PIXI.ParticleContainer(100, {
      scale: true,
      position: true,
      rotation: true,
      uvs: true,
      alpha: true
    })
    
    const emitter = new PIXI.particles.Emitter(
      explosion,
      [PIXI.Texture.WHITE],
      {
        alpha: {
          start: 1,
          end: 0
        },
        scale: {
          start: 1,
          end: 0.2,
          minimumScaleMultiplier: 1
        },
        color: {
          start: 'ffffff',
          end: 'ff0000'
        },
        speed: {
          start: 300,
          end: 100,
          minimumSpeedMultiplier: 1
        },
        acceleration: {
          x: 0,
          y: 0
        },
        maxSpeed: 0,
        startRotation: {
          min: 0,
          max: 360
        },
        noRotation: false,
        rotationSpeed: {
          min: 0,
          max: 0
        },
        lifetime: {
          min: 0.5,
          max: 1
        },
        blendMode: 'add',
        frequency: 0.001,
        emitterLifetime: 0.5,
        maxParticles: 100,
        pos: {
          x: position.x,
          y: position.y
        },
        addAtBack: false,
        spawnType: 'circle',
        spawnCircle: {
          x: 0,
          y: 0,
          r: 20
        }
      }
    )
    
    emitter.emit = true
    this.scene.addChild(explosion)
    this.effects.push({
      container: explosion,
      emitter,
      update: (delta) => {
        emitter.update(delta * 0.01)
        if (emitter.emitterLifetime > 0 && emitter.emitterLifetime <= delta * 0.01) {
          this.scene.removeChild(explosion)
        }
      }
    })
    this.playSound('explosion', 1.0)
    return explosion
  }

  // 创建地形交互效果
  createTerrainEffect(position, type) {
    const effect = new PIXI.Graphics()
    let color = 0xFFFFFF
    let alpha = 0.3
    
    switch(type) {
      case 'snow':
        color = 0xFFFFFF
        alpha = 0.5
        break
      case 'grass':
        color = 0x00FF00
        alpha = 0.3
        break
      case 'sand':
        color = 0xFFD700
        alpha = 0.4
        break
    }
    
    effect.beginFill(color, alpha)
    effect.drawCircle(0, 0, 50)
    effect.endFill()
    effect.position.set(position.x, position.y)
    
    // 添加动画
    effect.animate = (delta) => {
      effect.alpha = alpha + Math.sin(Date.now() * 0.002) * 0.1
      effect.scale.x = 1 + Math.sin(Date.now() * 0.001) * 0.05
      effect.scale.y = 1 + Math.cos(Date.now() * 0.001) * 0.05
    }
    
    this.scene.addChild(effect)
    this.effects.push(effect)
    return effect
  }

  // 更新特效
  update(delta) {
    this.effects.forEach(effect => {
      if (effect.update) {
        effect.update(delta)
      }
    })
  }

  // 清理特效
  clear() {
    this.effects.forEach(effect => {
      this.scene.removeChild(effect)
    })
    this.effects = []
  }
}
